package cn.dancepeak.controller;

import cn.dancepeak.domain.Student;
import cn.dancepeak.service.IStudentService;
import cn.dancepeak.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学生信息统一管理控制层
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private IStudentService studentService;

    /**
     * 查询学生的所有信息
     * @return R<list<Student>>  学生信息的集合
     */
    @PostMapping("/list")
    public R<List<Student>> getStudent(){
        List<Student> list =  studentService.getStudent();
        return R.ok(list);
    }

    /**
     * 新增一条学生信息
     * @param student
     * @return
     */
    @PostMapping("/add")
    public R addStudent(@RequestBody Student student){
        try {
            studentService.addStudent(student);
            return R.ok("新增成功！");
        }catch (Exception e){
            e.printStackTrace();
            return R.fail("mmm");
        }
    }

    /**
     * 修改一条学生信息
     * @param student
     * @return
     */
    @PostMapping("/update")
    public R updateStudent(@RequestBody Student student){
        studentService.updateStudent(student);
        return R.ok("修改成功！");
    }

    /**
     * 通过id删除一条学生信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R deleteStudentById(@PathVariable Long id){
        try {
            studentService.deleteStudentById(id);
            return R.ok("删除成功！");
        }catch (Exception e){
            e.printStackTrace();
            return R.fail();
        }

    }

}
